import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path:"/",
    redirect:"/login"
  },{
    path:"/login",
    component:()=>import("@/components/login.vue")
  },{
    path:"/home",
    component:()=>import("@/components/home.vue"),
    redirect:"/welcome",
    children:[
      {
        path:"/welcome",
        component:()=>import("@/components/welcome.vue")
      },
      {
        path:"/users",
        component:()=>import("@/components/users/users.vue")
      },{
        path:"/roles",
        component:()=>import("@/components/power/roles.vue")
      },{
        path:"/rights",
        component:()=>import("@/components/power/rights.vue")
      },
      {
        path:"/categories",
        component:()=>import("@/components/goods/categories.vue")
      },
      {
        path:"/goods",
        component:()=>import("@/components/goods/goods.vue")
      },
      {
        path:"/params",
        component:()=>import("@/components/goods/params.vue")
      },
      {
        path:"/orders",
        component:()=>import("@/components/orders/orders.vue")
      },
      {
        path:"/reports",
        component:()=>import("@/components/reports/reports.vue")
      },
    ]
  }
]

const router = new VueRouter({
  routes
})
// 路由拦截
const token = localStorage.getItem("login-token")
  router.beforeEach((to,from ,next)=>{
    if(!token&&to.path=='/welcome'){
       return  next("/login")
    }
    next()
  })

export default router
